package com.ml.loan.common;

import com.ml.loan.sys.model.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.util.Set;

/**
 * Controller基类
 * Created by wangfan on 2017-6-10 上午10:10
 */
public class BaseController {

    /**
     * 获取当前登录的user
     */
    public User getLoginUser() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null && subject.getPrincipals() != null) {
            Set<String> realms = subject.getPrincipals().getRealmNames();
            for (String realmName : realms) {
                if (realmName.contains("UserRealm")) {
                    //返回后台用户信息
                    Object object = subject.getPrincipal();
                    if (object != null) {
                        return (User) object;
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取当前登录的userId
     */
    public Integer getLoginUserId() {
        User loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getUserId();
    }

    /**
     * 获取当前登录的username
     */
    public String getLoginUserName() {
        User loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getUsername();
    }

    public String getLoginNickName() {
        User loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getNickName();
    }

}
